#============================================================
#	Plugin
#============================================================
#  折叠按钮
#============================================================
# @datetime: 2022-5-14 16:50:03
#============================================================
tool
extends EditorPlugin


# Meta 数据
const META_SIDEBAR_KEY = "TabContainerSideBar"
# 布局
const PATH_LAYOUT = "res://layout.cfg"

var gd_editor_node : GDEditorNode
var sidebars : Array = []


#============================================================
#   内置
#============================================================
func _enter_tree():
	print("正在加载 TabContainer侧边栏 插件中...")
	call_deferred("init_data")
	print("加载完成")


func _exit_tree():
	# 还原默认的样子
	if sidebars.size() == 0:
		# 使用这种方式防止特殊原因导致的 sidebar 变量丢失
		sidebars = get_viewport().get_meta(META_SIDEBAR_KEY)
	for sidebar in sidebars:
		sidebar.restore()
	for sidebar in sidebars:
		sidebar.queue_free()
	get_viewport().remove_meta(META_SIDEBAR_KEY)


func get_window_layout(layout: ConfigFile):
	if ResourceLoader.exists(PATH_LAYOUT):
		layout.load(PATH_LAYOUT)
		print("Get布局")
	else:
		layout.save(PATH_LAYOUT)
		print("保存布局")


#============================================================
#   自定义
#============================================================
func init_data():
	if not ResourceLoader.exists(PATH_LAYOUT):
		queue_save_layout()
	else:
		var layout = ConfigFile.new()
		layout.load(PATH_LAYOUT)
		set_window_layout(layout)
	gd_editor_node = GDEditorNode.new()
	
	if get_viewport().has_meta(META_SIDEBAR_KEY):
		sidebars = get_viewport().get_meta(META_SIDEBAR_KEY)
	
	get_viewport().set_meta(META_SIDEBAR_KEY, sidebars)
	
	
	var sub_panel_list = [
		gd_editor_node.editor_sub_panel[0],
		gd_editor_node.editor_sub_panel[1],
		gd_editor_node.editor_sub_panel[3],
		gd_editor_node.editor_sub_panel[4],
	]
	# 在对应的停靠栏上替换节点
#	sidebars.clear()
#	for i in sub_panel_list.size():
#		var sidebar : TabContainerSideBar
#		sidebar = TabContainerSideBar.new()
#		sidebars.append(sidebar)
#		
#		var sub_panel : Control = sub_panel_list[i]
#		for tab_container in sub_panel.get_children():
##			var tab_container : TabContainer = sub_panel.get_child(0)
#			if tab_container.get_child_count() > 0:
#				sidebar.set_tab_container(tab_container)
#				if not sidebar.is_inside_tree():
#					get_editor_interface().get_viewport().add_child(sidebar)
#				sidebar.change_to_fold_button()
	
	# 获取最左侧的 TabContainer
	var sub_panel : Control = gd_editor_node.editor_sub_panel[0]
	var tab_container_list = []
	for panel in sub_panel_list:
		for tab_container in panel.get_children():
			tab_container_list.append(tab_container)
	
	var sidebar = TabContainerSideBar.new()
	sidebars.append(sidebar)
	sidebar.set_tab_container(tab_container_list.front())
	if not sidebar.is_inside_tree():
		get_editor_interface().get_viewport().add_child(sidebar)
	
	for tab_container in tab_container_list:
		sidebar.show_tab_container_node(tab_container.get_children())
	
	


